package com.yingluo.abacus.plantform.docs.consumer.feign;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yingluo.abacus.plantform.docs.consumer.utils.DocsConsumerUtils;
import com.yingluo.abacus.plantform.utils.PlantformUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//http://localhost:8090/abacus/plantform/docs-consumer/consumer/resttemplate
@RestController
public class RestTemplateControler {
    private static final Logger logger = LoggerFactory.getLogger(RestTemplateControler.class);

    private final static String PATH = DocsConsumerUtils.MOUDLE_PATH + "/consumer/resttemplate";
    @Autowired
    RestTemplateService service;

    @RequestMapping(PlantformUtils.PROJECT_PATH +PATH)

    public String execute() {

        String str = service.hiService();

        logger.debug("======" + str);
        return str;
    }

}

@Service
class RestTemplateService {

    @Value("${spring.application.host-service}")
    String hostService;

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService() {
        return restTemplate.getForObject("http://" + hostService + "/abacus/cloud/service-provider/hello", String.class);
    }

    public String hiError() {
        return "sorry, cloud-service-web was error ,Hystrix !";
    }
}
